#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
const static int NUM=3;
typedef void(*func_t)();

func_t task[NUM];


void printfNAME()
{
    printf("this is print name\n");
}
void printfNPC()
{
    printf("this is print npc\n");
}
void printfAGE()
{
    printf("this is print age\n");
}

void InitLog()
{
    task[0]=printfNAME;
    task[1]=printfNPC;
    task[2]=printfAGE;
    task[3]=NULL;
}
void Exe()
{
    for(int i=0;task[i];i++)
    {
        task[i]();
    }
}
int main()
{
    InitLog();
    pid_t id = fork();
    if (id == 0)
    {
        // child
        int cnt = 5;
        while (cnt)
        {
            printf("I am child,pid: %d,ppid: %d\n", getpid(), getppid());
            cnt--;
            sleep(1);
        }
        sleep(12);
        exit(111);
    }
    // father
    int status=0;
    pid_t rid = waitpid(id,&status,WNOHANG);
    while(1)
    {
        if(rid>0)
        {
            printf("wait sucess,rid: %d,status: %d,exit code: %d\n",rid,status,WEXITSTATUS(status));
            break;
        }
        else if(rid==0)
        {
            printf("child is running,do other thing\n");
            //开始任务
            printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
            Exe();
        }
        else
        {
            perror("waitpid");
            break;
        }
        sleep(1);
    }

    return 0;
}

// for(int i=0;i<200;i++)
// {
//     printf("%d: %s\n",i,strerror(i));
// }
